package cn.stacey.blog.web.demo;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.stacey.blog.domain.AjaxResponse;
import cn.stacey.blog.domain.Category;
import cn.stacey.blog.domain.Post;
import cn.stacey.blog.service.PostService;
import cn.stacey.blog.utils.ServiceProxyUtils;

//,"/post/Page"
@WebServlet(urlPatterns= {"/post/findAll","/post/findPage","/post/deleteBatch","/post/search","/post/addPost"})
public class PostServlet extends BaseServlet{
	//PostService service = new PostService();
	//现在用动态代理对象创建一个postservice
	PostService service=(PostService) ServiceProxyUtils.createProxy(new PostService());
	
	//??出过错误在这里，找了很久，发现是下面的方法是protected而不是public
	public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		List<Post> data=service.findAll();
		
		//分页，浏览器会传来两个数据，一个page(第几页),一个limit(这一页有多少内容)
		//在数据库中的查询语句
	//select * from post limit 11(第二页从序号为11的开始),10(第二页有10条)
		//因为浏览器传来的第一个参数与数据库查询的不对应，进行转化：第2页对应的数据库第11条
		//select * from post limit (page-1)*limit limit
		
		
		
		//在ajax对象中定义了code msg  count
		//将以下代码抽取到POstServlet的父类中的writeList方法中去
//				AjaxResponse<Post> ajax = new AjaxResponse<>();
//				ajax.setCode(0);
//				ajax.setMsg("");
//				ajax.setData(data);
//				String string = JSON.toJSONString(ajax);
//				resp.setContentType("application/json;charset=utf-8");
//				resp.getWriter().write(string);
//				System.out.println("findAll");
		writeListData(resp, data);
	}
	


	public void findPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		PostService service = new PostService();
		int page = Integer.valueOf((req.getParameter("page")));
		int limit=Integer.valueOf(req.getParameter("limit"));
		AjaxResponse<Post> data = service.findPage(page , limit);
		//响应给服务器成功的消息
		data.setCode(0);
		data.setMsg("success");
		writePageData(resp, data);
	}
	
	public void deleteBatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//取前端传来的多个id
		String[] ids = req.getParameterMap().get("ids");
		service.deleteBatch(ids);
	}
	
	public void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		//String title,int cid,String created,int page,int size
		String title = req.getParameter("title");
		String cid1=(req.getParameter("category"));
		//要判断是否分类为空（因为有的文章没有分类）
		Integer cid=(cid1==null||cid1.length()==0)?null:Integer.valueOf(cid1);	
		String created=req.getParameter("created");
		int page=Integer.valueOf(req.getParameter("page"));
		int size=Integer.valueOf(req.getParameter("limit"));
		AjaxResponse<Post> data = service.searchPage(title, cid, created, page, size);
		data.setCode(0);
		data.setMsg("success");
		writePageData(resp, data);
		
		
		
	}
	
	public void addPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		Category category=new Category();
		Post post=new Post();
		String category_id = req.getParameter("category");
		Integer cid = Integer.valueOf(category_id);
//		if(category_id!=null) {
//		int cateid=Integer.valueOf(category_id);
//		category.setId(cateid);
//		int cid=category.getPriority();
//	System.out.println(cid);
//		post.setCid(cid);
//		}

		

		post.setTitle(title);
		post.setContent(content);
		post.setCid(cid);
		post.setCreated(new Date());
		service.addPost(post);
	}
	
			
}
